package tw.asap.plus.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ProgressBar;

import tw.asap.plus.R;
import tw.asap.plus.common.ConstValue;
import tw.asap.plus.presenter.LogoutPresenter;
import tw.asap.plus.presenter.LogoutPresenterImpl;
import tw.asap.plus.presenter.LogoutView;
import tw.asap.plus.ui.BaseActivity;
import tw.asap.plus.ui.custom.AppBar;
import tw.asap.plus.ui.custom.SwitchButton;
import tw.asap.plus.utils.AppManager;


/**
 * Created by chenfeiyue on 15/7/30.
 */
public class SettingActivity extends BaseActivity implements LogoutView, View.OnClickListener {

    private SwitchButton switchButton;
    private ProgressBar progressBar;
    private LogoutPresenter presenter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_setting);
        presenter = new LogoutPresenterImpl(this);
        initView();
    }

    private void initView(){
        switchButton = (SwitchButton) findViewById(R.id.switchButton);
        boolean offlineDev = spUtil.getBoolean(ConstValue.Sp.OFFLINE_DEV, false);
        switchButton.setChecked(offlineDev);
        switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                spUtil.save(ConstValue.Sp.OFFLINE_DEV, isChecked);
            }
        });
        setHasAppBar(true);
        getAppBar().setLeftNavigation(R.drawable.btn_back_red, new AppBar.OnLeftClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getAppBar().setShowRight(false);
        getAppBar().setTitle("设置");
        progressBar = (ProgressBar) findViewById(R.id.progress);

        findViewById(R.id.button).setOnClickListener(this);
    }



    @Override
    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void onFailed(String resperr) {
        progressBar.setVisibility(View.GONE);
        closeWebActivity();
    }

    @Override
    public void onSuccess() {
        progressBar.setVisibility(View.GONE);
        closeWebActivity();
    }

    @Override
    public Context getContext() {
        return SettingActivity.this;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                presenter.logout();
                break;
        }
    }


    private void closeWebActivity(){
        spUtil.save(ConstValue.Sp.SESSIONID, "");
        spUtil.save(ConstValue.Sp.PASSWORD, "");
        spUtil.save(ConstValue.Sp.COOKIE, "");
        AppManager.getAppManager().finishActivity(WebActivity.class);
        startActivity(new Intent(SettingActivity.this, LoginActivity.class));
        finish();
    }
}